#require 'file_size_validator'

class Fighter < ActiveRecord::Base
  has_merit

  belongs_to :user
  belongs_to :ladder
  has_many :competitions
  has_many :matches, :through => :competitions

  mount_uploader :avatar, AvatarUploader
  mount_uploader :poster, PosterUploader

  extend FriendlyId
  friendly_id :slug_candidates, use: :slugged
  def slug_candidates
    [
      [:name, :nickname],
      [:name, :nickname, :city],
    ]
  end

  #validates :avatar, 
  #      :presence => true , 
  #      :file_size => { :maximum => 5.megabytes.to_i} 
  #validates_integrity_of :avatar
  #validates_processing_of :avatar   

  attr_accessor :crop_img, :crop_x, :crop_y, :crop_w, :crop_h
  after_update :crop_image

  def crop_image
    if crop_img == "avatar"
      avatar.recreate_versions! if crop_x.present?
    elsif crop_img == "poster"
      poster.recreate_versions! if crop_x.present?
    end
  end

  attr_accessor :height_feet, :height_inches
  before_save :combine_height if @feet.present?

  def height_feet=(feet)
    @feet = feet
  end

  def height_inches=(inches) #on save?
    @inches = inches
  end

  def combine_height
    self.height = @feet + "\' " + @inches + "\"" #if @feet.present?
  end
end
